<template>
  <div class="paper">
    <Title title='试题列表'/>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="Id">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="题干">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="分数">
        <template slot-scope="scope">
          <span>{{ scope.row.score }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handlePreview(scope.row.options, scope.row.name)"
            >预览</el-button
          >
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="preview-paper">
      <el-dialog title="预览" :visible.sync="dialogVisible" width="50%">
        <PreviewSubject :data="subjects" :name="name"/>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getAllSubjects, delSubject } from "@/api/paper";
import { formatTime, error, success } from "@/utils";
import PreviewSubject from "@/components/preview/PreviewSubject.vue";
import Title from '@/components/common/Title'
export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      subjects: [],
      name: ""
    };
  },
  components: {
      PreviewSubject,
      Title
  },
  methods: {
    // 获取试题
    async getData() {
      // 获取所有的试题
      const res = await getAllSubjects();
      this.tableData = formatTime(res.data);
    },
    /**
     * 预览
     */
    handlePreview(data, name) {
      this.subjects = data
      this.name = name
      this.dialogVisible = true
    },
    /**
     * 删除
     */
    handleDelete(id) {
       this.$confirm("此操作将永久删除该题目,是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async _ => {    
          const res = await delSubject(id)
          if (res.code === 1) {
              success(res.msg)
              this.getData()
          } else {
              error(res.msg)
          }
      }).catch(_ => {

      })
    },
  },
  created() {
    this.getData();
  },
};
</script>

<style lang="scss" scoped></style>
